package com.ksyzt.ksite2.client.column;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gwt.uibinder.client.UiConstructor;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TreeItem;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.COLUMNObj;
import com.ksyzt.ksite2.shared.modle.COLUMNObjs;

public class ColumnList extends ListBox {

	@UiConstructor
	public ColumnList() {
		super();
	}

	List<COLUMNObj> m_list;
	private AsyncCallback<COLUMNObjs> on_root_data = new AsyncCallback<COLUMNObjs>() {

		public void onSuccess(COLUMNObjs result) {
			m_map.clear();

			parseRoot(result);
		}

		public void onFailure(Throwable caught) {

			Window.alert(caught.getMessage());
		}
	};

	private void getChildItem(COLUMNObj o) {
		ServerProxy.SYS_MANAGER
				.getAllColumnChildrens(o.getID(), on_all_chidren);
	}

	TreeItem m_root;

	private void parseRoot(COLUMNObjs result) {
		if (result.size() > 0) {
			for (int i = 0; i < result.size(); i++) {
				COLUMNObj o = result.get(i);
				TreeItem ti = new TreeItem("0");
				ti.setUserObject(o);
				m_map.put(o.getID(), ti);
				m_root = ti;
				getChildItem(o);
			}
		}
	}

	public COLUMNObj getSelectColumn() {
		int index = this.getSelectedIndex();
		if (index >= 0) {
			String v = this.getValue(index);
			Integer oid = Integer.valueOf(v);
			TreeItem ti = m_map.get(oid);
			if (ti != null) {
				return (COLUMNObj) ti.getUserObject();
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

	void redraw(TreeItem ti) {

		if (ti != null) {
			COLUMNObj o = (COLUMNObj) ti.getUserObject();

			int space = Integer.valueOf(ti.getHTML());
			String str = "";
			for (int i = 0; i < space; i++) {
				str += "  ";
			}
			this.addItem(str + o.getNAME(), o.getID() + "");

			for (int i = 0; i < ti.getChildCount(); i++) {
				TreeItem tic = ti.getChild(i);
				redraw(tic);
			}
		}
	}

	private AsyncCallback<COLUMNObjs> on_all_chidren = new AsyncCallback<COLUMNObjs>() {

		public void onSuccess(COLUMNObjs result) {

			for (int i = 0; i < result.size(); i++) {
				COLUMNObj o = result.get(i);
				TreeItem ti = m_map.get(o.getPID());

				int space = Integer.valueOf(ti.getHTML());
				if (ti != null) {
					TreeItem tic = ti.addItem("" + (space + 1));
					m_map.put(o.getID(), tic);
					tic.setUserObject(o);
				}
			}

			ColumnList.this.clear();
			redraw(m_root);
		}

		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());

		}
	};

	Map<Integer, TreeItem> m_map = new HashMap<Integer, TreeItem>();

	int m_userid = -1;
	int m_type = -1;

	/**
	 * 初始化组织List
	 *
	 * @param userid
	 *            该组织树所属用户ID
	 * @param orgtype
	 *            要编辑的组织类型ID
	 */
	public void initList(int userid, int orgtype) {
		if (m_userid == userid && m_type == orgtype) {

		} else {
			m_userid = userid;
			m_type = orgtype;
			this.clear();
			ServerProxy.SYS_MANAGER.getColumns(userid, orgtype, on_root_data);
		}
	}

	public void refresh() {
		if (m_userid >= 0 && m_type >= 0) {
			this.clear();
			ServerProxy.SYS_MANAGER.getColumns(m_userid, m_type, on_root_data);
		}
	}
}
